var map;
var marker;
var inforWindow;
var arr;
var num=0;
var lineArr = [];
var timer;
var speed=12000;
var isplay=false;
function hidewarn_ts(){
	
	  $("#warn_ts").hide()
}


function initialize(){
	
	 var position=new AMap.LngLat(116.397428,39.90923);
	 
	  map = new AMap.Map("map_here", {  
		  center: position,  
		  level: 15  
	  }); 
	  map.plugin(["AMap.ToolBar"],function(){		
			toolBar = new AMap.ToolBar();
			map.addControl(toolBar);
	  });
	  map.plugin(["AMap.Scale"],function(){		
			scale = new AMap.Scale();
			map.addControl(scale);
	  });
	
	  shuaxin();
	 
}
function playback(){
	
	if(isplay){	
		isplay=false;
    	clearTimeout(timer);
    	marker.stopMove();
    	$("#start").removeClass("guiji_button_zt");
    	$("#start").addClass("guiji_button_bf");
    	
	}else{
    	goon();
    	isplay=true;
    	$("#start").removeClass("guiji_button_bf");
    	$("#start").addClass("guiji_button_zt");
	}
}
function timeout(){
	
	
}
function goon(){
	var json = '[{"p2":0,"p1":0,"state":"停车超时(14时14分)","veo":0,"lng":108.91175079345703,"oil":0,"id":608991,"temp1":"127.0","temp2":"127.0","temp3":"127.0","dir":295,"dis":0,"posinfo":"陕西省 西安市 雁塔区 电子城街道 电子一路273号 电子一路东 西北税务学校南98米","lat_xz":-0.0015780000248923898,"lat":34.21770095825195,"lng_bi":0.01106099970638752,"gpst":"2017-05-13 09:00:29","photo":"","av":"1","temp4":"127.0","totaldistance":14418.7138671875,"lat_bi":0.0046709999442100525,"lng_xz":0.004658000078052282,"recvt":"2017-05-13 09:00:29","temp":0}]';
	var arr = eval(json);
	
	if(num < lineArr.length){
		 //$( "#slider" ).slider( "option", "value", num );
		 var position=lineArr[num];
		 var obj = getVhcIcon(arr[num].state,
			arr[num].veo,
			arr[num].dir,
			arr[num].recvt,
			arr[num].av,
			0,1);
		 var size = getIconSize(0);
		 
		 inforWindow = new AMap.InfoWindow({
					  isCustom:true,
					  closeWhenClickMap:true,
					  autoMove:false,
				      position:position,
					  //size:new AMap.Size(250, 0),                
					  offset:new AMap.Pixel(2,-20),
					  content:"<div class='tanchu_wrap_map'>"
							+"<div class='tanchu_main'>"
							+"<span><b>终端名称：</b>"+decodeURI($("#tername").val())+"</span>"
							+"<span><b>接收时间：</b>"+arr[num].recvt+"</span>"
							+"<span><b>速度：</b>"+arr[num].veo.toFixed(1)+" km/h</span>"
							+"<span><b>方向：</b>"+obj.dir+"</span>"
							//+"<span><b>油量：</b>"+json.data[0].positionLast.oilMN1+" L</span>"
							//+"<span><b>当日里程：</b>"+json.data[0].positionLast.distance+" 公里</span>"
							//+"<span><b>总里程：</b>"+json.data[0].positionLast.totalDistance+" 公里</span>"
							+"<span><b>状态：</b>"+arr[num].state+"</span>"
							+"<span><b>位置：</b>"+arr[num].posinfo+"</span>"
							+"</div>"
							+"<div class='tauchu_arrow_main'>"
							+"<span class='tanchu_arrow'></span>"
							+"<span class='tanchu_arrow_1'></span> </div></div>"                  
					});   
		 
		 marker.on('click',function(e){
		   			inforWindow.open(map,marker.getPosition());        
				});
				marker.on('moveend',function(e){
	    			 goon();
	 	 	    });
				marker.on('moving',function(e){
					//map.setCenter(marker.getPosition());
					
					inforWindow.setPosition(marker.getPosition());
					if(! map.getBounds().contains(marker.getPosition())){
						map.setCenter(marker.getPosition());
					}
					
		 	    });
				inforWindow.open(map,marker.getPosition());       
				
				$("#loading_wrap").hide()
				isplay = true;
				num++;
				timer=setTimeout(goon,100);
				//map.setCenter(position);
		 if(marker == null){
			 
		    	marker = new AMap.Marker({                  
					  map:map, //将点添加到地图  
					  position:position,                  
					  icon:obj.icon,//marker图标，直接传递地址url                  
					  offset:new AMap.Pixel(-(size/2),-(size/2)) //相对于基点的位置       
				});
		   		          
		   		marker.on('click',function(e){
		   			inforWindow.open(map,marker.getPosition());        
				});
				marker.on('moveend',function(e){
	    			 goon();
	 	 	    });
				marker.on('moving',function(e){
					//map.setCenter(marker.getPosition());
					
					inforWindow.setPosition(marker.getPosition());
					if(! map.getBounds().contains(marker.getPosition())){
						map.setCenter(marker.getPosition());
					}
					
		 	    });
				inforWindow.open(map,marker.getPosition());       
				
				$("#loading_wrap").hide()
				isplay = true;
				num++;
				timer=setTimeout(goon,100);
				//map.setCenter(position);
		    }else{
			 	marker.setIcon(obj.icon);
				marker.setPosition(position);
				inforWindow.setContent("<div class='tanchu_wrap_map'>"
						+"<div class='tanchu_main'>"
						+"<span><b>终端名称：</b>"+decodeURI($("#tername").val())+"</span>"
						+"<span><b>接收时间：</b>"+arr[num].recvt+"</span>"
						+"<span><b>速度：</b>"+arr[num].veo.toFixed(1)+" km/h</span>"
						+"<span><b>方向：</b>"+obj.dir+"</span>"
						//+"<span><b>油量：</b>"+json.data[0].positionLast.oilMN1+" L</span>"
						//+"<span><b>当日里程：</b>"+json.data[0].positionLast.distance+" 公里</span>"
						//+"<span><b>总里程：</b>"+json.data[0].positionLast.totalDistance+" 公里</span>"
						+"<span><b>状态：</b>"+arr[num].state+"</span>"
						+"<span><b>位置：</b>"+arr[num].posinfo+"</span>"
						+"</div>"
						+"<div class='tauchu_arrow_main'>"
						+"<span class='tanchu_arrow'></span>"
						+"<span class='tanchu_arrow_1'></span> </div></div>"); //在信息窗体中显示新的信息内容
				//inforWindow.open(map,position);
				//inforWindow.setPosition(position)
				num++;
				if(Math.abs(lineArr[num].getLng( ) - lineArr[num-1].getLng( )) < 0.0001 && Math.abs(lineArr[num].getLat( ) - lineArr[num-1].getLat( )) < 0.0001 ){
					timer=setTimeout(goon,100);
				}else{
				  marker.moveTo(lineArr[num], speed);
				}
		    }
		    
		
	}
	
  

}

function shuaxin(){
	var lngX = 116.397428, latY = 39.90923;        
			   for (var i = 1; i < 4; i++) {
			        lngX = lngX + Math.random() * 0.05;
			        if (i % 2) {
			            latY = latY + Math.random() * 0.0001;
			        } else {
			            latY = latY + Math.random() * 0.06;
			        }
			        lineArr.push([lngX, latY]);
			    }
			   var polyline=new AMap.Polyline({
		            map:map,
		            path:lineArr,
		            strokeColor:"#0000ee",//线颜色
		            strokeOpacity:0.8,//线透明度
		            strokeWeight:3,//线宽
		            strokeStyle:"solid",//线样式
		        });
		       map.setFitView(polyline);
		       $( "#slider" ).slider( "option", "max", json.topics.length-1);
		       goon()
	
}

function getIconSize(iconid){
	if(iconid == 0 || iconid == 9  ){
		
		return 32;
		
	}else{
		return 24;
	}
}
function getVhcIcon(state,speed,dir,time,av,icon,isPlaybakc)
{
			var s=10;
			var condition=state;
		  	var sp = parseInt(speed);
		  	var direction="";
			var t = parseInt(dir);
		  	if (condition != null){
				if (condition.indexOf("不在线")>=0 || condition.indexOf("掉线")>=0)
					s = 2;
				else {
					if (condition.indexOf("速报警")>=0)
						s = 4;
					else if (condition.indexOf("报警")>=0)
						s = 3;
					else if (sp > 0.5)
						s = 0;
					else
						s = 1;
				}
				if (s != 4 && s != 3 && (condition.indexOf("停车")>=0 || condition.indexOf("停止")>=0 )
						&& !condition.indexOf("不在线")>= 0)
					{
					
						if(condition.indexOf("停车超时")>=0 || condition.indexOf("静止超时")>=0 || condition.indexOf("超时停车")>=0){
							s = 5;	
						}else{
							s = 6;
						}
					}
			}else{
				if (sp > 0.5)
					s = 0;
				else
					s = 1;
			}
			var newtime = new Date().getTime();
			var ttime =time.replace("-","/");
			ttime=ttime.replace("-","/");
			ttime=ttime.replace("T"," ");
			var oldtime = new Date(ttime).getTime();
			if(newtime-oldtime > 600000){
				if(isPlaybakc == null) s=2;
			}
			
			if( av == 0 ){
				s=7;
			}
		 	var state =s;
		 	var image;
		 	var pngicon;
		 	
		 	pngicon = "../images/pic/"+icon+"/red_";
				if(state == 0) pngicon = "../images/pic/"+icon+"/green_";
				else if(state == 1) pngicon = "../images/pic/"+icon+"/red_";
				else if(state == 2) pngicon = "../images/pic/"+icon+"/gray_";
				else if(state == 3) pngicon = "../images/pic/"+icon+"/yellow_";
				else if(state == 4) pngicon = "../images/pic/"+icon+"/pink_";
				else if(state == 5) pngicon = "../images/pic/"+icon+"/stopalarm";
				else if(state == 7) pngicon = "../images/pic/"+icon+"/blue_";
				else pngicon = "../images/pic/"+icon+"/red_";
		 
				
			  if(t>27 && t<=72){
			  		if(state == 5){
			  			 image=".gif";
			  		}else{
			  			 image="45.gif";
			  		}
					
					direction="东北";
				}else if(t>72 && t<=117){
					if(state == 5){
			  			 image=".gif";
			  		}else{
			  			 image="90.gif";
			  		}
					
					direction="东";
				}else if(t>117 && t<=162){
					if(state == 5){
			  			 image=".gif";
			  		}else{
			  			 image="135.gif";
			  		}
					direction="东南";
				}
				else if(t>162 && t<=207){
					if(state == 5){
			  			 image=".gif";
			  		}else{
			  			 image="180.gif";
			  		}
					direction="南";
				}
			  
				else if(t>207 && t<=252){
					if(state == 5){
			  			 image=".gif";
			  		}else{
			  			 image="225.gif";
			  		}
					direction="西南";
				}
				else if(t>252 && t<=297){
					if(state == 5){
			  			 image=".gif";
			  		}else{
			  			 image="270.gif";
			  		}
					direction="西";
				}
				else if(t>297 && t<=342){
					if(state == 5){
			  			 image=".gif";
			  		}else{
			  			 image="315.gif";
			  		}
					direction="西北";
				}
				else {
					if(state == 5){
			  			 image=".gif";
			  		}else{
			  			 image="0.gif";
			  		}
					direction="北";
				}
				
			 	if(state==1 || state == 5 ){
			 		direction="静止";
			 	}
			 	var iconObj = new Object();
			 	iconObj.dir=direction;
			 	iconObj.icon=pngicon+image;
			 	iconObj.state=state;
			 	return iconObj;
			 	
}